package tech.aistar.corejava.day13;

import java.io.*;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/8/2
 * @Version: 1.0
 */
public class ByteIODemo {
    public static void main(String[] args) {
        copyFile("src/tech/aistar/corejava/day13/FileDemo.java","src/tech/aistar/corejava/day13/FileDemo_副本.java");
    }

    private static void copyFile(String src,String target) {
        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(target);

            //定义一个缓冲区数组
            byte[] buf = new byte[3*1024];
            //循环
            while(true){
                //in.read(buf) - 从输入流读取的字节存储到buf字符数组中.
                //len - 代表的是读取的字节的大小
                int len = in.read(buf);

                if (len == -1) {
                    break;
                }
                    //System.out.println(len);

                    //将buf写出去,从0位置取完写出去len个字节
                    out.write(buf,0,len);

                    System.out.println("文件拷贝!");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if(null != out){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        //
    }
}
